package com.moyun.common.event.dict.concretehandler.dictitem;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moyun.model.vo.system.DictItemOptions;
import com.moyun.model.vo.system.DictTypeOptions;
import com.moyun.model.po.dict.SysDict;
import com.moyun.model.po.dict.SysDictItem;
import com.moyun.service.dict.SysDictItemService;
import com.moyun.service.dict.SysDictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Slf4j
@Component
public class MyDictItemHandler extends DefaultDictItemHandler {

    @Autowired
    private SysDictService sysDictService;
    @Autowired
    private SysDictItemService sysDictItemService;

    @Override
    public Set<DictTypeOptions> getDictTypeList() {
        // 从数据库中获取 字典类型数据 集合
        List<SysDict> list = this.sysDictService.list();
        HashSet<DictTypeOptions> dictTypeOptions = CollectionUtil.newHashSet();
        for (SysDict sysDict : list) {
            DictTypeOptions dictTypeOptionsTemp = new DictTypeOptions();
            dictTypeOptionsTemp.setTypeCode(sysDict.getCode());
            dictTypeOptionsTemp.setTypeName(sysDict.getName());
            dictTypeOptions.add(dictTypeOptionsTemp);
        }
        return dictTypeOptions;
    }

    @Override
    public List<DictItemOptions> getDictItemOptions(String typeCode) {
        QueryWrapper<SysDictItem> sysDictItemQueryWrapper = new QueryWrapper<>();
        sysDictItemQueryWrapper.in("type", typeCode);
        List<SysDictItem> list = this.sysDictItemService.list(sysDictItemQueryWrapper);
        List<DictItemOptions> dictItemOptionsList = new ArrayList<>();
        for (SysDictItem sysDictItem : list) {
            DictItemOptions dictItemOptionsTemp = new DictItemOptions();
            dictItemOptionsTemp.setTypeCode(sysDictItem.getType());
            dictItemOptionsTemp.setDataLabel(sysDictItem.getLabel());
            dictItemOptionsTemp.setDataValue(sysDictItem.getValue());
            dictItemOptionsList.add(dictItemOptionsTemp);
        }
        return dictItemOptionsList;
    }

    @Override
    public List<DictItemOptions> getDictItemOptions(Collection<String> typeCodeList) {
        // 根据指定的 typeCodeList 获取 对应的 字典项数据
        List<DictItemOptions> dictItemOptionsList = new ArrayList<>();
        QueryWrapper<SysDictItem> sysDictItemQueryWrapper = new QueryWrapper<>();
        sysDictItemQueryWrapper.in("type", typeCodeList);
        List<SysDictItem> list = this.sysDictItemService.list(sysDictItemQueryWrapper);
        for (SysDictItem sysDictItem : list) {
            DictItemOptions dictItemOptionsTemp = new DictItemOptions();
            dictItemOptionsTemp.setTypeCode(sysDictItem.getType());
            dictItemOptionsTemp.setDataLabel(sysDictItem.getLabel());
            dictItemOptionsTemp.setDataValue(sysDictItem.getValue());
            dictItemOptionsList.add(dictItemOptionsTemp);
        }
        return dictItemOptionsList;
    }
}
